package iedcyt.acute.view;

import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import iedcyt.acute.businessServices.cache.domain.ErrorLog;
import iedcyt.acute.controller.ErrorLogController;
import iedcyt.acute.view.widgets.ErrorLogTable;

public class ErrorLogView {

	private Shell _localShell;
	private ErrorLogController _controller;
	
	private ErrorLogTable _contentTable;
	private Button _prev;
	private Button _next;
	
	public ErrorLogView() {
		
		ViewUtils.getInstance().getShell().setEnabled(false);
					
		_localShell = new Shell(ViewUtils.getInstance().getShell().getDisplay(), SWT.CLOSE | SWT.RESIZE);

		_controller = new ErrorLogController(this);
						
		try	{
			Image shellIcon = new Image(_localShell.getDisplay(), "warn.gif");
			_localShell.setImage(shellIcon);
		} catch (Exception e) {
			//si falla la carga del icono el programa debe seguir pudiendo ser utilizado.
		}

		_localShell.setText("Registro de errores");
		_localShell.setMinimized(false);
		
		_contentTable = new ErrorLogTable(_localShell,SWT.BORDER);
		_contentTable.addListener(SWT.Selection, _controller.getErrorLogSelectionListener());
		_contentTable.addListener(SWT.KeyDown, _controller.getErrorLogKeyDownListener());
	
		_prev = new Button(_localShell, SWT.PUSH | SWT.HORIZONTAL);
        _prev.setText("<<");
        _prev.addSelectionListener(_controller.getPrevSelectionListener());
		
        _next = new Button(_localShell, SWT.PUSH | SWT.HORIZONTAL);
        _next.setText(">>");
        _next.addSelectionListener(_controller.getNextSelectionListener());
        
        setPositionOfVisualElements();
        
		_controller.iniView();
											
	}
	
	private void setPositionOfVisualElements() {
		// Posicionamos los objetos del composite
		// x, y, width, height
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		_localShell.setLayout(gridLayout);

		GridData gD_comp = new GridData(GridData.FILL, GridData.FILL, true,
				true);
		_localShell.setLayoutData(gD_comp);
	
		GridData gD_btnPrev = new GridData(GridData.BEGINNING,
				GridData.CENTER, false, false);
		gD_btnPrev.horizontalSpan = 1;
		_prev.setLayoutData(gD_btnPrev);

		GridData gD_btnNext = new GridData(GridData.BEGINNING,
				GridData.CENTER, false, false);
		gD_btnNext.horizontalSpan = 1;
		_next.setLayoutData(gD_btnNext);

		GridData gD_contentTable = new GridData(GridData.FILL, GridData.FILL,
				true, true);
		gD_contentTable.horizontalSpan = 2;
		gD_contentTable.widthHint = 500;
		gD_contentTable.heightHint = 650;
		_contentTable.setLayoutData(gD_contentTable);
		
	}
	
	public void closeWindow() {
		_localShell.close();
		ViewUtils.getInstance().getShell().setEnabled(true);
	}
	
	public void openWindow() {
		_localShell.pack();
		_localShell.addDisposeListener(_controller.getCloseListener());
		_localShell.open();
	}
	
	public void setErrors(List<ErrorLog> list) {
		_contentTable.removeAll();
		_contentTable.setElements(list);
	}
	
	/**
	 * Posiciona la shell centrada en medio de la pantalla
	 */
	public void moveLocalShellToCenter() {
		Monitor primary = _localShell.getDisplay().getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = _localShell.getBounds();
	    
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    
	    _localShell.setLocation(x, y);
	}

	public Integer getSelectedErrorID() {
		if (_contentTable.getSelection(0) != null) {
			return Integer.valueOf(_contentTable.getSelection(0).getText(0));
		}
		return null;
	}
	
}
